﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace UserDAL
{
    public static class UserServices
    {
        public static UserData[] myFriendsObject(int myid)
        {
            MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call myFriends(@userid)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userid", myid);
            DataTable myDataSet = new DataTable("user");
            myDataAdapter.Fill(myDataSet);
            if (myDataSet.Rows.Count > 0)
            {
                UserData[] myFriends = new UserData[myDataSet.Rows.Count];
                for (int i = 0; i < myDataSet.Rows.Count; i++)
                    myFriends[i] = new UserData((int)myDataSet.Rows[i].ItemArray[0], (string)myDataSet.Rows[i].ItemArray[1], (float)myDataSet.Rows[i].ItemArray[2], (string)myDataSet.Rows[i].ItemArray[3], (string)myDataSet.Rows[i].ItemArray[4]);
                return myFriends;
            }
            else
                return null;
        }

        public static DataTable myFriends(int myid){
            MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call myFriends(@userid)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userid", myid);
            DataTable myDataSet = new DataTable("user");
            myDataAdapter.Fill(myDataSet);
            return myDataSet;
        }


        public static void log(int userid, string log)
        {
            MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call Log(@userid,@logText,@logdate1)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userid", userid);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@logText", log);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@logdate1", DateTime.Now.ToShortDateString()+" "+ DateTime.Now.ToShortTimeString());
            DataTable myDataSet = new DataTable("log");
            myDataAdapter.Fill(myDataSet);
        }

        public static List<string> getLog(int userid, int nlimit)
        {
            MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call getLog(@userid,@nlimit)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userid", userid);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@nlimit", nlimit);
            DataTable myDataSet = new DataTable("log");
            myDataAdapter.Fill(myDataSet);
            if (myDataSet.Rows.Count > 0)
            {
                List<string> logList = new List<string>();
                foreach (DataRow x in myDataSet.Rows)
                {
                    logList.Add((string)x["log"]);
                    logList.Add((string)x["logdate"]);
                }
                return logList;
            }
            return null;
        }
    }
}
